package com.sakura.probe.component;

import com.sakura.probe.nosql.redis.RedisKey;
import com.sakura.probe.nosql.redis.RedisService;
import com.sakura.probe.pojo.Comment;
import com.sakura.probe.pojo.Topic;
import com.sakura.probe.service.CommentService;
import com.sakura.probe.service.TopicService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Iterator;
import java.util.Set;

/**
 * 定期更新帖子的点赞和收藏情况
 */
@Component
@Slf4j
public class CounterTask {
    @Autowired
    private RedisService redisService;
    @Autowired
    private TopicService topicService;
    @Autowired
    private CommentService commentService;

    /**
     * 每10秒定期更新话题点赞量
     */
//    @Scheduled(cron = "0/10 * * * * ?")
//    private void countThumb(){
//        //log.info("每10秒定期更新话题点赞量");
//        Set<String> topicIdSet = redisService.getSet(RedisKey.getThumbTopicIdList());
//        if (null!=topicIdSet){
//            //log.info("更新话题点赞量");
//            Topic topic;
//            for (String topicId:topicIdSet){
//                //获取需要更新点赞量的话题id
//                Long num = Long.valueOf(redisService.get(RedisKey.getThumbNum() + topicId));
//                redisService.remove(RedisKey.getThumbNum() + topicId);
//                topic = topicService.getById(topicId);
//                //更新数据
//                topic.setTopicThumbNum(topic.getTopicThumbNum()+num);
//                topicService.updateById(topic);
//            }
//        }
//    }

    /**
     * 每40秒定期更新话题收藏量
     */
//    @Scheduled(cron = "0/40 * * * * ?")
//    private void countCollect(){
//        //log.info("每40秒定期更新话题收藏量");
//        Set<String> topicIdSet = redisService.getSet(RedisKey.getCollectTopicIdList());
//        if (null!=topicIdSet&&!topicIdSet.isEmpty()){
//            //log.info("更新话题收藏量");
//            Topic topic;
//            for (String topicId:topicIdSet){
//                Long num = Long.valueOf(redisService.get(RedisKey.getCollectNum() + topicId));
//                redisService.remove(RedisKey.getCollectNum() + topicId);
//                topic = new Topic();
//                topic.setTopicId(Long.valueOf(topicId));
//                //更新数据
//                topic.setTopicDiscussNum(topic.getTopicDiscussNum()+num);
//                topicService.updateById(topic);
//            }
//        }
//
//    }

    /**
     * 每20秒定期更新评论量
     */
//    @Scheduled(cron = "0/20 * * * * ?")
//    private void countComment(){
//        //log.info("每20秒定期更新评论量");
//        Set<String> topicIdSet = redisService.getSet(RedisKey.getCommentTopicIdList());
//        if (null!=topicIdSet&&!topicIdSet.isEmpty()){
//            Topic topic;
//            Comment comment;
//            for (String topicId:topicIdSet){
//                comment = new Comment();
//                comment.setTopicId(Long.valueOf(topicId));
//                topic = new Topic();
//                //直接从mysql中统计总评论数并更新话题的评论数
//                topic.setTopicDiscussNum(commentService.countByCondition(comment));
//                topic.setTopicId(Long.valueOf(topicId));
//                //更新数据
//                topicService.updateById(topic);
//            }
//        }
//    }
}













